﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Jayden.Dll.Web.HtmlControls;
using Jayden.Dll.Entities.Common;
using Jayden.Dll.Entities.Enums;

namespace Jayden.Site.PopjesInDeZak.Dll
{
	public class PopjesDiv : PopjesPagedDiv
	{

		private int m_PageIndex = 0;
		private int m_PageSize = 12;
		private int m_TotalRecords = -1;

		private Ol m_PopjesList;
		private Ol m_PagesList;

		public override void Load(System.Xml.XmlNode node, Jayden.Dll.Web.HtmlControlLoader loader)
		{
			base.Load(node, loader);

			m_PopjesList = GetElementByServerId<Ol>("popjesList", true);
			m_PagesList = GetElementByServerId<Ol>("pagesList", true);
		}

		public void ProcessRequestWithContext(Jayden.Dll.Web.HttpHandlerContext context)
		{
			ProductDataAccess productDataAccess = context.UnitOfWork.DataAccessManager.GetDataAccess<Product>() as ProductDataAccess;
			if(productDataAccess != null)
			{
				m_PopjesList.Clear();
				m_PagesList.Clear();
				List<Product> products = productDataAccess.ReadByCategory(null, m_PageIndex, m_PageSize, out m_TotalRecords);
				foreach (Product popje in products)
					CreatePopjeItem(popje);

				int pages = (m_TotalRecords / m_PageSize) + ((m_TotalRecords % m_PageSize) != 0 ? 1 : 0);
				for (int c = 0; c <= pages; c++)
				{
					A link = m_PagesList.AddLi().AddA((c + 1).ToString(), "?page=" + c);
					if (m_PageIndex == c)
						link.Classes.Add("selected");
				}

				if (m_PageIndex == 0)
					m_Previous.CssProperties.Add("display", "none");
				if (m_PageIndex == pages)
					m_Next.CssProperties.Add("display", "none");
			}
		}

		public override void ProcessHtmlRequest(Jayden.Dll.Web.HttpHandlerContext context)
		{
			base.ProcessHtmlRequest(context);

			ProcessRequestWithContext(context);
		}

		protected string GetPopjeStatus(ProductStatusType status)
		{
			if (status == ProductStatusType.Available)
				return "";
			else if (status == ProductStatusType.OutOfStock)
				return "Out of stock";
			else if (status == ProductStatusType.Reserved)
				return "Reserved";
			return string.Empty;
		}

		protected Li CreatePopjeItem(Product popje)
		{
			Li li = m_PopjesList.AddLi();
			Div imageDiv = li.AddControl<Div>();
			A imageLink = imageDiv.AddControl<A>();
			Img image = null;
			if (popje != null)
				image = imageLink.AddImg(popje.Label, popje.Thumbnail);
			else
				image = imageLink.AddImg();
			Div details = li.AddControl<Div>();
			Div name = details.AddControl<Div>();
			Div status = details.AddControl<Div>();

			imageDiv.Classes.Add("picture");
			image.Classes.Add("popje");
			details.Classes.Add("details");
			name.Classes.Add("name");
			status.Classes.Add("status");

			if (popje != null)
			{
				imageLink.HRef = "/details.html?product=" + popje.ProductId;
				image.Src = popje.Thumbnail;
				name.InnerText = popje.Label;
				status.InnerText = GetPopjeStatus(popje.StatusType);
			}
			return li;
		}

	}
}